Why Formal Verification Matters:
Benefits of K Framework:
Pre-requisites:
Installation Steps:
Install Binaries from:
https://github.com/runtimeverification/k/releases/tag/v7.1.155
Verify Installation:
kompile --version
K Definition Structure:
Smart Contract Representation:
Goal: Prove that a smart contract meets its specification without error.
Steps to Formal Verification:
Problem Statement:
Basic Contract Logic:
function transfer(address sender, address receiver, uint amount) public {
require(balance[sender] >= amount, "Insufficient funds");
balance[sender] -= amount;
balance[receiver] += amount;
}
Key Properties to Verify:
Define Syntax:
syntax Contract ::= "transfer" "(" Address "," Address "," Int ")"
syntax Int ::= balance(Address)
Define Rules:
rule <k> transfer(S, R, A) => . ... </k>
<balance> (S |-> BS => BS - A)
(R |-> BR => BR + A)
...
requires BS >= A
Property to Verify (Balance Preservation):
claim <k> transfer(S, R, A) => . ... </k>
<balance> (S |-> BS) (R |-> BR) => <balance> (S |-> BS - A) (R |-> BR + A)
ensures BS + BR == BS' + BR'
Compile the Contract:
kompile contract.k
Prove Properties:
krun --prove contract-verification-spec.k
Output: